package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.CompleteStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgGroupBuying;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgGroupBuyingService;

@Controller
@RequestMapping("/admin/groupbuying")
public class GroupBuyingController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private YgGroupBuyingService ygGroupBuyingService;
	@Autowired
	private RedisDao redisDao;

	@RequestMapping("searchGroupBuying")
	@ResponseBody
	public Map<String, Object> searchGroupBuying(Long id, QueryInfo queryInfo, String userName, Date startDate, Date endDate, String groupName,
			CompleteStatus completeStatus) {

		ResultSet<YgGroupBuying> resultSet = ygGroupBuyingService.search(queryInfo, id, userName, startDate, endDate, groupName, completeStatus);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("getNewCusGroup")
	@ResponseBody
	public Message getNewCusGroup() {
		String rediskey = "New_Custom_Group_Product";
		String string = redisDao.get(rediskey);
		return Message.success(string);
	}

	@RequestMapping("setNewCusGroup")
	@ResponseBody
	public Message setNewCusGroup(String productIds) {
		Assert.notNull(productIds);
		String rediskey = "New_Custom_Group_%s";
		String[] split = productIds.split(",");
		Long[] pIds = (Long[]) ConvertUtils.convert(split, Long.class);
		List<Long> skuIds = ygGroupBuyingService.findSkuIdsByPids(pIds);
		String skuStr = StringUtils.join(skuIds, ",");
		redisDao.set(String.format(rediskey, "Product"), productIds, 0);
		if (StringUtils.isBlank(skuStr)) {
			skuStr = "0";
		}
		redisDao.set(String.format(rediskey, "Sku"), skuStr, 0);
		return Message.success(null);
	}

}
